<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
        <el-row>
            <el-col>
                <el-button type="primary" style="margin-left: 10px"
                @click="showAddCateDialog()">添加分类</el-button>
            </el-col>
        </el-row>
        <!-- 表格数据 -->
        <tree-table :data="catelist"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        show-index
        index-text="#"
        border
        class="treeTable">
          <!-- 定义isok模板 -->
          <template slot="isok" slot-scope="scope">
            <i v-if="scope.row.cat_deleted===false" class="el-icon-success"
            style="color: lightgreen"></i>
            <i v-else class="el-icon-error"
            style="color: red"></i>
          </template>
          <!-- 定义order模板 -->
          <template slot="order" slot-scope="scope">
            <el-tag size="mini" v-if="scope.row.cat_level===0">一级</el-tag>
            <el-tag size="mini" v-else-if="scope.row.cat_level===1" type="success">二级</el-tag>
            <el-tag size="mini" v-else type="warning">三级</el-tag>
          </template>
          <!-- 定义opt模板 -->
          <template slot="opt" slot-scope="scope">
            <el-button  size="mini" type="primary" class="el-icon-edit"
            @click="editCate(scope.row)">编辑</el-button>
            <el-button  size="mini" type="danger" class="el-icon-delete"
            @click="deleteCate(scope.row)">删除</el-button>
          </template>
        </tree-table>
        <!-- 分页 -->
        <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 5, 10, 30]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
        </el-pagination>
    </el-card>
    <!-- 添加分类对话框 -->
    <el-dialog
    title="添加分类"
    :visible.sync="addCateDialogVisible"
    width="50%"
    @close="addCateDialogClose()"
    >
    <el-form :model="addCateForm" :rules="addCateRules" ref="addCateFormRef" label-width="100px" class="demo-ruleForm">
        <el-form-item label="分类名称:" prop="cat_name">
            <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级名称:">
            <el-cascader
                clearable
                v-model="selectedKeys"
                :options="parentCateList"
                :props="cascaderProps"
                @change="parentCateChange">
            </el-cascader>
        </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
    </span>
    </el-dialog>
    <!-- 编辑分类对话框 -->
    <el-dialog
    title="编辑分类"
    :visible.sync="editCateDialogVisible"
    width="50%"
    >
    <el-form ref="editCateFormRef" label-width="100px" class="demo-ruleForm">
        <el-form-item label="分类名称:">
            <el-input v-model="editCateList.cat_name"></el-input>
        </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
        <el-button @click="editCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmEditCate">确 定</el-button>
    </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
    name: 'CategoryPage',
    data(){
        return{
            // 商品分类列表
            catelist: [],
            // 获取商品分类列表接口数据对象
            queryInfo: {
                type: 3,        // 数据层级
                pagenum: 1,     // 当前页面
                pagesize: 5     // 页面数据
            },
            // 总页数
            total: 0,
            // 树形表格列数据
            columns: [
                {label: '分类名称',prop: 'cat_name'},
                // 定义为模板列,模板名称为isok,用于isok插槽
                {label: '是否有效',type: 'template',template: 'isok'},
                {label: '排序',type: 'template',template: 'order'},
                {label: '操作',type: 'template',template: 'opt'},
            ],
            // 添加分类对话框显示
            addCateDialogVisible: false,
            // 添加分类表单数据对象
            addCateForm: {
                // 需添加的商品分类名称
                cat_name: '',
                // 父级分类id
                cat_pid: 0,
                // 分类等级 默认一级
                cat_level: 0,
            },
            // 添加分类表单验证规则
            addCateRules: {
                cat_name: [
                    { required: true, message: '请输入分类名称', trigger: 'blur' }
                ]
            },
            // 父级分类数据列表
            parentCateList: [],
            // 级联选择器配置对象
            cascaderProps: {
                expandTrigger: 'hover',
                value: 'cat_id', // 选中的值
                label: 'cat_name',  // 显示的内容
                children: 'children',  // 嵌套所用的属性
                checkStrictly: true     // 子选项非必选
            },
            // 所选的分类的所有id
            selectedKeys: [],
            editCateDialogVisible: false,
            editCateList: {},
            // 删除
            deleteCateList: {}
        }
    },
    created(){
        this.getCateList()
    },
    methods: {
        async getCateList(){
            const{data:res} = await this.$http.get('categories',{
                params: this.queryInfo
            })
            if(res.meta.status !== 200) return this.$message.error('获取商品数据失败')
            console.log(res.data);
            this.catelist = res.data.result
            this.total = res.data.total     // 总条数
        },
        // 页面条数发生改变
        handleSizeChange(newSize){
            this.queryInfo.pagenum = 1
            this.queryInfo.pagesize = newSize
            this.getCateList()
        },
        // 当前页面发生改变
        handleCurrentChange(newPage){
            this.queryInfo.pagenum = newPage
            this.getCateList()  
        },
        // 添加分类对话框显示
        showAddCateDialog(){
            this.getParentCateList()
            this.addCateDialogVisible = true
        },
        // 获取父级分类数据列表
        async getParentCateList(){
            const {data:res} = await this.$http.get('categories',{params: {
                type: 2
            }})
            if(res.meta.status !== 200) return this.$message.error('获取父级分类数据失败')
            console.log(res.data)
            this.parentCateList = res.data
        },
        // 级联选择器发生改变时
        parentCateChange(){
            console.log(this.selectedKeys)
            // 选择了父级分类内容时
            if(this.selectedKeys.length >= 0){
                this.addCateForm.cat_pid = this.selectedKeys[this.selectedKeys.length-1]
                this.addCateForm.cat_level = this.selectedKeys.length
                return
            }else{
                this.addCateForm.cat_pid = 0
                this.addCateForm.cat_level = 0
            }
        },
        // 确认添加分类
        addCate(){
            console.log(this.addCateForm)
            this.$refs.addCateFormRef.validate(async valid => {
                if(!valid) return
               const {data:res} = await this.$http.post('categories',this.addCateForm)
                if(res.meta.status !== 201) return this.$message.error('添加分类错误')
                this.$message.success('添加分类成功')
                this.getCateList()
                this.addCateDialogVisible = false
            })
        },
        // 添加分类对话框关闭
        addCateDialogClose(){
            // 格式化表单与级联选择器的内容
            this.$refs.addCateFormRef.resetFields()
            this.selectedKeys = []
            this.addCateForm.cat_level = 0
            this.addCateForm.cat_pid = 0
            // 更新后台数据

        },
        // 编辑分类
        editCate(list){
            this.editCateDialogVisible = true
            this.editCateList = list
            console.log(this.editCateList)
        },
        // 确定编辑分类
        async confirmEditCate(){
            const {data:res} = await this.$http.put(`categories/${this.editCateList.cat_id}`,
            {cat_name: this.editCateList.cat_name})
            if(res.meta.status !== 200) return this.$message.error('编辑分类错误')
            this.$message.success('编辑分类成功')
            this.getCateList()
            this.editCateDialogVisible = false
        },
        // 删除分类
        deleteCate(list){
            this.deleteCateList = list
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
            }).then(async () => {
                const{data:res} = await this.$http.delete(`categories/${this.deleteCateList.cat_id}`)
                if(res.meta.status !== 200) return this.$message.error('分类删除失败')
                this.$message.success('删除成功')
                this.getCateList()
            })
        }
    }
}
</script>

<style lang="less" scoped>
.treeTable{
    margin: 15px;
}

</style>